package org.xxpay.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xxpay.core.common.constant.RetEnum;
import org.xxpay.core.common.exception.ServiceException;
import org.xxpay.core.entity.AlipayCash;
import org.xxpay.core.entity.AlipayCashDetail;
import org.xxpay.core.entity.AlipayCashDetailExample;
import org.xxpay.core.entity.AlipayCashExample;
import org.xxpay.core.service.IAlipayCashService;
import org.xxpay.service.dao.mapper.AlipayCashDetailMapper;
import org.xxpay.service.dao.mapper.AlipayCashMapper;

import java.util.Arrays;
import java.util.List;

@Service
public class AlipayCashServiceImpl implements IAlipayCashService {

    @Autowired
    private AlipayCashMapper recordMapper;

    @Autowired
    private AlipayCashDetailMapper alipayCashDetailMapper;

    @Override
    public List<AlipayCash> select(int pageIndex, int pageSize, AlipayCash alipayCash) {

        AlipayCashExample exa = getFullExample(alipayCash);
        exa.setOrderByClause("CreateTime desc");
        exa.setOffset(pageIndex);
        exa.setLimit(pageSize);
        return recordMapper.selectByExample(exa);
    }

    @Override
    public int count(AlipayCash alipayCash) {
        return recordMapper.countByExample(getFullExample(alipayCash));
    }

    @Override
    public AlipayCash findById(Integer id) {
        return recordMapper.selectByPrimaryKey(id);
    }

    @Override
    public int add(AlipayCash alipayCash) {
        return 0;
    }

    @Override
    public int update(AlipayCash alipayCash) {
        return recordMapper.updateByPrimaryKeySelective(alipayCash);
    }

    @Override
    @Transactional(transactionManager = "transactionManager", rollbackFor = Exception.class)
    public Integer save(AlipayCash alipayCash, List<AlipayCashDetail> details) {

        recordMapper.insertSelective(alipayCash);

        for(AlipayCashDetail detail : details){
            detail.setCashId(alipayCash.getId());
            alipayCashDetailMapper.insertSelective(detail);
        }
        return alipayCash.getId();

    }

    @Override
    @Transactional(transactionManager = "transactionManager", rollbackFor = Exception.class)
    public void cancel(Integer cashId) {

        AlipayCash updateRecord = new AlipayCash();
        updateRecord.setTaskStatus(AlipayCash.TASK_STATUS_CANCEL);

        AlipayCashExample condition = new AlipayCashExample();
        condition.createCriteria().andIdEqualTo(cashId).andTaskStatusIn(Arrays.asList(AlipayCash.TASK_STATUS_CREATE_SUCCESS, AlipayCash.TASK_STATUS_ING));
        Integer updateRow = recordMapper.updateByExampleSelective(updateRecord, condition);
        if(updateRow <= 0){
            throw new ServiceException(RetEnum.RET_COMM_OPERATION_FAIL);
        }

        AlipayCashDetail updateRecordDetail = new AlipayCashDetail();
        updateRecordDetail.setStatus(AlipayCashDetail.STATUS_CANCEL);
        AlipayCashDetailExample detailCondition = new AlipayCashDetailExample();
        detailCondition.createCriteria().andCashIdEqualTo(cashId).andStatusEqualTo(AlipayCashDetail.STATUS_WAIT);
        updateRow = alipayCashDetailMapper.updateByExampleSelective(updateRecordDetail, detailCondition);
        if(updateRow <= 0){
            throw new ServiceException(RetEnum.RET_COMM_OPERATION_FAIL); //保证事物回滚
        }
    }

    private AlipayCashExample getFullExample(AlipayCash record){

        if(record == null) return new AlipayCashExample();
        AlipayCashExample exa = new AlipayCashExample();
        AlipayCashExample.Criteria c = exa.createCriteria();
        if(record.getId() != null) c.andIdEqualTo(record.getId());
        if(record.getTaskStatus() != null) c.andTaskStatusEqualTo(record.getTaskStatus());
        if(record.getAccountTemplateId() != null) c.andAccountTemplateIdEqualTo(record.getAccountTemplateId());
        if(!StringUtils.isEmpty(record.getPrizeType())) c.andPrizeTypeEqualTo(record.getPrizeType());

        return exa;
    }

}
